using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using WL.Think.Abp.Modules;

namespace WL.Think.Abp;

public class ThinkDbMigratorStartup
{
    public static async Task StartupAsync<TModule>(string[] args) where TModule: ThinkModule
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .MinimumLevel.Override("Volo.Abp", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Async(c => c.Console())
            .CreateLogger();

        await Host
                .CreateDefaultBuilder(args)
                .AddAppSettingsSecretsJson()
                .ConfigureLogging((context, logging) => logging.ClearProviders())
                .ConfigureServices((hostContext, services) => {
                    services.Configure<ThinkDbMigratorOptions>(options =>
                    {
                        options.StartupModuleType = typeof(TModule);
                    });
                    services.AddHostedService<ThinkDbMigratorHostedService>();
                })
                .RunConsoleAsync();
    }
}
